package com.carkeeper.serve.service.impl;

import com.carkeeper.pojo.Serve;
import com.carkeeper.pojo.ServeDetail;
import com.carkeeper.serve.dao.ServeDetailMapper;
import com.carkeeper.serve.dao.ServeMapper;
import com.carkeeper.serve.service.ServeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ServeServiceImpl implements ServeService {

    @Autowired
    private ServeMapper serveMapper;

    @Autowired
    private ServeDetailMapper serveDetailMapper;

    @Override
    public PageInfo<Serve> findByCondition(Map<String, String> searchMap) {
        Example example = new Example(Serve.class);
        Example.Criteria criteria = example.createCriteria();


        int pageSize = 20;
        int pageNum = 1;

        if (searchMap != null) {

            if (searchMap.get("startPage") != null && searchMap.get("pageSize") != null) {

                if (!"".equals(searchMap.get("startPage"))) {
                    pageNum = Integer.parseInt(searchMap.get("startPage"));
                }

                if (!"".equals(searchMap.get("pageSize"))) {
                    pageSize = Integer.parseInt(searchMap.get("pageSize"));
                }
            }

            //服务项目编号
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {

                criteria.andEqualTo("code", searchMap.get("code"));
            }
            //服务商名称
            if (searchMap.get("storeId") != null && !"".equals(searchMap.get("storeId"))) {

                criteria.andEqualTo("storeId", searchMap.get("storeId"));
            }
            //服务项目名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {

                criteria.andEqualTo("name", searchMap.get("name"));
            }
            //服务分类
            if (searchMap.get("type2") != null && !"".equals(searchMap.get("type2"))) {

                criteria.andGreaterThanOrEqualTo("type2", searchMap.get("type2"));
            }
        }
        PageHelper.startPage(pageNum,pageSize);

        List<Serve> serveList = serveMapper.selectByExample(example);


        PageInfo<Serve> pageInfo = new PageInfo<>(serveList);
        return pageInfo;
    }

    @Override
    public List findByType2() {
        List list = serveMapper.findByType2();
        return list;
    }

    @Override
    public ServeDetail findByDateil(String id) {
        ServeDetail serveDetail = serveDetailMapper.selectByPrimaryKey(id);
        return serveDetail;
    }

    /**
     * 下架
     * @param id
     */
    @Override
    public void down(String id) {
        Serve serve = serveMapper.selectByPrimaryKey(id);
        if (serve!=null){
            if (!"1".equals(serve.getStatus())){
                serve.setStatus("1");
            }
        }
        serveMapper.updateByPrimaryKeySelective(serve);
    }
}